<style lang="scss" scoped>

</style>

<template>
    <div class="infobody">
        <dealer-topbar :title="title" @changeStatus="changeStatus" @delItem="delItem" @saveItem="saveItem"></dealer-topbar>
        <div class="content">
            <p class="form-title">角色信息</p>
            <div class="info">
                <Form :model="roleInfo" >
                    <FormItem  label="名称" >
                        <Input :disabled="status"  v-model="roleInfo.roleName" placeholder="请输入名称" />
                    </FormItem>
                </Form>
            </div>
        </div>
    </div>
</template>

<script>
import dealerTopbar from "@/views/component/dealerTopbar.vue";
import {sampleData} from 'api/index.js';
export default {
  data() {
    return {
      title: "角色",
      status: true,
      roleInfo: {
        roleId:"",
        roleName: ""
      }
    };
  },
  methods: {
    changeStatus(param) {
      this.status = param.btnEdit;
    },
    saveItem(){
      if (this.$store.state.agentOperation == "edit") {
        //调更新接口
        let obj=this.roleInfo;
        sampleData.roleUpdate(obj).then(res=>{
          if(res.code==0||res.code==200){
            this.$Message.success("修改成功")
            this.$router.push('/roleList')
          }else{
            this.$Message.error(res.msg)
          }
        }).catch(error=>{
          this.$Message.error(error)
        })
      } else {
        //调新增接口
        let obj=this.roleInfo;
        sampleData.roleAdd(obj).then(res=>{
          if(res.code==0||res.code==200){
            this.$Message.success("添加成功")
            this.$router.push('/roleList')
          }else{
            this.$Message.error(res.msg)
          }
        }).catch(error=>{
          this.$Message.error(error)
        })
      }      
    },
    delItem(){
    //   let obj={
    //     roleId:this.$route.query.roleId
    //   }
      let obj=[]
      obj.push(this.$route.query.roleId)
      sampleData.roleDelete(obj).then(res=>{
        if(res.code==0||res.code==200){
          this.$Message.success("删除成功");
          this.$router.push('/roleList')
        }else{
          this.$Message.error(res.msg)
        }
      }).catch(error=>{
        this.$Message.error(error);
      })      
    },
    getRoleById(){
        console.log(this.$route.query.roleId)
      let obj={
        roleId:this.$route.query.roleId
      }
      sampleData.getRoleById(obj).then(res=>{
        if(res.code==0||res.code==200){
          this.roleInfo=res.role;
        }else{
          this.$Message.error(res.msg)
        }
      }).catch(error=>{
        this.$Message.error(error);
      })
    }
  },
  mounted() {
    if (this.$store.state.agentOperation == "edit") {
      this.status = true;
      this.getRoleById();
    } else {
      this.status = false;
    }
  },
  components: {
    dealerTopbar
  }
};
</script>